package gui;

import gates.Factory;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * Small options panel in the top right of the UI. Used to set static Factory variables.
 * @author Matt Cole
 *
 */
public class OptionsPanel extends JPanel implements ItemListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Checkbox object for basic gates */
	JCheckBox gatesCheckBox;
	/** Radiobutton object for functional gates */
	JRadioButton functionalRadButton;
	/** Radiobutton object for matrix gates */
	JRadioButton matrixRadButton;
	/** Size of box to display options */
	private final int widthAndHeight = 200;

	/** Constructor for the options panel */
	public OptionsPanel() {

		// Format the panel. Note that the height of the options panel is the height of the title panel.
		setPreferredSize(new Dimension(widthAndHeight, widthAndHeight));
		setLayout(new GridLayout(0, 1));
		setBackground(Color.white);
		setBorder(BorderFactory.createTitledBorder("Options"));

		// Basic gates check box
		gatesCheckBox = new JCheckBox("Use Basic Gates");
		gatesCheckBox.addItemListener(this);
		gatesCheckBox.setBackground(Color.white);

		// Functional gates radio button
		functionalRadButton = new JRadioButton("Functional Gates");
		functionalRadButton.addItemListener(this);
		functionalRadButton.setBackground(Color.white);
		functionalRadButton.setSelected(true);

		// Matrix gates radio button
		matrixRadButton = new JRadioButton("Matrix Gates");
		matrixRadButton.addItemListener(this);
		matrixRadButton.setBackground(Color.white);

		// Group the radio buttons
		ButtonGroup radioButtons = new ButtonGroup();
		radioButtons.add(functionalRadButton);
		radioButtons.add(matrixRadButton);

		// Add all options buttons to the panel
		add(gatesCheckBox);
		add(matrixRadButton);
		add(functionalRadButton);

	}

	/**
	 * Dealing with the events of the buttons being changed. 
	 * Changes static booleans in the Factory class
	 */
	public void itemStateChanged(ItemEvent e) {
		Object source = e.getItemSelectable();
		if(source == gatesCheckBox) {
			if (e.getStateChange() == ItemEvent.SELECTED) {
				Factory.groverbasicgates = true;
			}
			else if (e.getStateChange() == ItemEvent.DESELECTED) {
				Factory.groverbasicgates = false;
			}
		}
		else if(source == matrixRadButton) {
			if (e.getStateChange() == ItemEvent.SELECTED) {
				Factory.functional = false;
			}
			else if (e.getStateChange() == ItemEvent.DESELECTED) {
				Factory.functional = true;
			}
		}
		else if(source == functionalRadButton) {
			if (e.getStateChange() == ItemEvent.SELECTED) {
				Factory.functional = true;
			}
			else if (e.getStateChange() == ItemEvent.DESELECTED) {
				Factory.functional = false;
			}
		}
	}

	/**
	 * Resets to the default options.
	 */
	public void reset() {
		gatesCheckBox.setSelected(false);
		functionalRadButton.setSelected(false);
	}

	/**
	 * Return the size of one side of the square options panel.
	 * @return widthAndHeight box width/height of panel
	 */
	public int getWidthAndHeight() {
		return widthAndHeight;
	}
}
